package com.ruiboyun.liveapp.util.tool;

import android.graphics.Color;
import android.os.Build;
import android.support.design.widget.Snackbar;
import android.support.v4.content.ContextCompat;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ruiboyun.liveapp.R;
import com.ruiboyun.liveapp.base.App;


/**
 * Created by wsq on 2016/8/10.
 */
public class SnackbarUtils {

   public static Snackbar showLong(View view, String msg){
       Snackbar snackbar= Snackbar.make(view,msg, Snackbar.LENGTH_SHORT);
       snackbar.show();
       return snackbar;
   }

    public static void showSnackBar(View view,String msg){
        if (view!=null&&view.getContext()!=null){
            showSnackBar(view,msg, ContextCompat.getColor(App.getInstance(), R.color.colorPrimary));
        }

    }

    public static void showSnackBar(View view,String msg,int backgroundColor){

        try {
            //1.创建snackbar
            Snackbar snackbar= Snackbar.make(view,msg, Snackbar.LENGTH_SHORT);
            //2.获取snackbar的布局layout
            Snackbar.SnackbarLayout snackbarLayout= (Snackbar.SnackbarLayout) snackbar.getView();
            //3.设置snackbar的背景颜色
            snackbarLayout.setBackgroundColor(backgroundColor);
            //4.获取snackbar的TextView
            TextView textView= (TextView) snackbarLayout.findViewById(R.id.snackbar_text);
            //设置字体颜色
            textView.setTextColor(Color.WHITE);
            //设置字体居中
            if (Build.VERSION.SDK_INT>16){
                textView.setTextAlignment(View.TEXT_ALIGNMENT_GRAVITY);
                textView.setGravity(Gravity.CENTER);
            }
            //设置snackbar的显示位置
            ViewGroup.LayoutParams vl = snackbarLayout.getLayoutParams();
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(vl.width,vl.height);
            params.gravity = Gravity.TOP;
            snackbarLayout.setLayoutParams(params);
            snackbar.show();
        }catch (Exception e){
            ToasUtil.showCenterToast(App.getInstance(),msg);
            e.printStackTrace();
            Loger.e(e.toString());
        }

    }

    public static void showBottomSnackBar(View view,String msg){
        Snackbar snackbar= Snackbar.make(view,msg, Snackbar.LENGTH_SHORT);
        Snackbar.SnackbarLayout snackbarLayout= (Snackbar.SnackbarLayout) snackbar.getView();
        snackbarLayout.setBackgroundColor(ContextCompat.getColor(App.getInstance(), R.color.colorPrimary));
        TextView textView= (TextView) snackbarLayout.findViewById(R.id.snackbar_text);
        textView.setTextColor(Color.WHITE);
        snackbar.show();
    }


}
